package com.ruoyi.client.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.client.mapper.AddressMapper;
import com.ruoyi.client.mapper.ClientOrderDetailMapper;
import com.ruoyi.client.mapper.ClientOrderMapper;
import com.ruoyi.client.pojo.form.OrderInsertForm;
import com.ruoyi.client.pojo.form.OrderItemInsertForm;
import com.ruoyi.client.pojo.vo.OrderDetailVo;
import com.ruoyi.client.pojo.vo.OrderVo;
import com.ruoyi.client.service.IClientOrderService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.xz.domain.Order;
import com.ruoyi.xz.domain.OrderDetail;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class ClientOrderServiceImpl implements IClientOrderService {
    @Resource
    private ClientOrderMapper clientOrderMapper;
    @Resource
    private AddressMapper addressMapper;
    @Resource
    private ClientOrderDetailMapper clientOrderDetailMapper;

    @Override
    public boolean create(OrderInsertForm form) {
        // 获取用户Id
        Long userId = StpUtil.getLoginIdAsLong();

        Order order = new Order();
        order.setUserId(userId);
        order.setAddressId(form.getAddressId());
        order.setRemark(form.getRemark());

        // 调用数据层,新增订单,如果出错，抛出异常
        if (clientOrderMapper.insert(order) <= 0) {
            return false;
        }

        //获取订单id
        //ClientOrderMapper.xml文件insert方法中使用useGeneratedKeys 此时order.id可以生成返回
        //useGeneratedKeys="true" keyProperty="id"
        Long orderId = order.getId();

        //生成订单项
        List<OrderDetail> orderDetailList = new ArrayList<>();
        for (OrderItemInsertForm item : form.getProductList()) {
            OrderDetail orderDetail = new OrderDetail();
            // 数据转换
            BeanUtils.copyProperties(item, orderDetail);
            orderDetail.setOrderId(orderId);
            orderDetailList.add(orderDetail);
        }
        //调用数据层，批量插入订单明细列表,返回结果
        return clientOrderDetailMapper.insertBatch(orderDetailList) > 0 ;
    }

    // 查询订单我的列表，包括每个订单的购买商品明细项
    @Override
    public List<OrderVo> list(Long userId) {
        // 调用数据层，获取我的订单列表
        List<OrderVo> orderVoList = clientOrderMapper.listVo(userId);

        // 遍历每个订单
        for(OrderVo orderVo:orderVoList){
            // 调用数据层，获取订单明细列表
            List<OrderDetailVo> orderDetailVoList = clientOrderDetailMapper.selectVoByOrderId(orderVo.getId());
            orderVo.setDetailList(orderDetailVoList);
        }
        return orderVoList;
    }

    // 查询订单详情，包括订单项明细列表
    @Override
    public OrderVo selectById(Long orderId) {
        // 调用数据层，获取对应订单
        OrderVo orderVo = clientOrderMapper.selectVoById(orderId);
        if (orderVo == null) {
            throw new ServiceException("id对应的订单不存在");
        }
        // 调用数据层，获取订单明细列表
        List<OrderDetailVo> orderDetailVoList = clientOrderDetailMapper.selectVoByOrderId(orderId);
        orderVo.setDetailList(orderDetailVoList);
        return orderVo;
    }

    // 取消订单 初始0/取消1/完成2
    @Override
    public boolean cancel(Long id) {
        return clientOrderMapper.cancel(id) > 0;
    }

    @Override
    public boolean delete(Long id) {
        return clientOrderMapper.delete(id) > 0;
    }
}
